package com.huitone.smspfm.oldclient.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.oldclient.logger.ExtLogger;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class DbSmsConf  {
    private final static ExtLogger logger = ExtLogger.create(DbSmsConf.class);
    private final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat dayDateFormat = new SimpleDateFormat("YYYY-MM-dd ");
    private String table;
    private List<ForbitTime> forbitTimes;

    public DbSmsConf() {}

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public List<ForbitTime> getForbitTimes() {
        return forbitTimes;
    }

    public void setForbitTimes(List<ForbitTime> forbitTimes) {
        this.forbitTimes = forbitTimes;
    }

    public boolean forbitTime(long time) {
        boolean falg = false;
        long current = System.currentTimeMillis();
        if (this.forbitTimes != null && !this.forbitTimes.isEmpty()){
            //当天开始结束时间
            //00:00:00.000
            long dayStartTime = current/(24*3600*1000)*(24*3600*1000)- TimeZone.getDefault().getRawOffset();
            //23:59:59.999
            long dayEndTime = dayStartTime+(24*3600*1000)-1;
            String dayStr = dayDateFormat.format(current);

            for (ForbitTime forbitTime: this.forbitTimes){
                long startTime = -1,endTime=-1;
                try {
                    startTime = dateFormat.parse(dayStr + forbitTime.start).getTime();
                    endTime = dateFormat.parse(dayStr + forbitTime.end).getTime();
                } catch (ParseException ex) {
                    ex.printStackTrace();
                    logger.warn(DbSmsConf.class.getName()+"日期格式有误 HH:mm:ss！ forbitTime： "+forbitTime.toString()+" ex: "+ex);
                }
                if (startTime < 0 || endTime < 0) {
                    continue;
                }

                //当天的时间
                if (endTime <= dayEndTime) {
                    //处于禁发时间
                    if (time >= startTime && time <= endTime) {
                        falg = true;
                        break;
                    }
                } else {
                //跨天时间区别
                    //处于禁发时间
                    if (time > startTime || time < endTime) {
                        falg = true;
                        break;
                    }
                }
            }
        }
        return falg;
    }


    public static DbSmsConf parseDbSmsConf(String jsontext){
        if (StringUtils.isEmpty(jsontext)){
            return null;
        }
        DbSmsConf dbSmsConf = null;
        try {
            JSONObject jsonObject = JSON.parseObject(jsontext);
            dbSmsConf = JSON.toJavaObject(jsonObject, DbSmsConf.class);
        } catch (Exception e) {
            logger.warn(DbSmsConf.class.getName()+"转换失败！ jsontext： "+jsontext+" ex: "+e);
        }
        return dbSmsConf;
    }

    public String parseToJson(){
        return JSONObject.toJSONString(this);
    }

    public static class ForbitTime  {
        private String start;
        private String end;

        public ForbitTime(){}
        public ForbitTime(String start, String end){
            this.start = start;
            this.end = end;
        }

        public String getStart() {
            return start;
        }

        public void setStart(String start) {
            this.start = start;
        }

        public String getEnd() {
            return end;
        }

        public void setEnd(String end) {
            this.end = end;
        }
    }

    public static void main(String[] args) throws ParseException {
        DbSmsConf dbSmsConf = new DbSmsConf();
        dbSmsConf.setTable("table01");
        List<ForbitTime> forbitTimes = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ForbitTime forbitTime = new ForbitTime((10+i)+":00:00",(11+i)+":00:00");
            forbitTimes.add(forbitTime);
        }
        dbSmsConf.setForbitTimes(forbitTimes);
        System.out.println(dbSmsConf.parseToJson());
        String jsontext = "{\"forbitTimes\":[{\"end\":\"11:00:00\",\"start\":\"10:00:00\"},{\"end\":\"12:00:00\",\"start\":\"11:00:00\"},{\"end\":\"13:00:00\",\"start\":\"12:00:00\"},{\"end\":\"14:00:00\",\"start\":\"13:00:00\"}],\"table\":\"table02\"}";
        DbSmsConf dbSmsConf1 = DbSmsConf.parseDbSmsConf(jsontext);
        System.out.println(dbSmsConf1.parseToJson());
        List<DbSmsConf> dbSmsConfs = new ArrayList<>();
        dbSmsConfs.add(dbSmsConf);
        dbSmsConfs.add(dbSmsConf1);

        long currentTime = System.currentTimeMillis();
        String day = "2021-06-02 01:00:00";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println(dbSmsConf.forbitTime(df.parse(day).getTime()));
    }
}
